package com.fick.hibernate.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.fick.hibernate.model.News;

/**
 * A data access object (DAO) providing persistence and search support for News
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.fick.hibernate.model.News
 * @author MyEclipse Persistence Tools
 */

public class NewsDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(NewsDAO.class);

	// property constants
	public static final String SMALLCLASSID = "smallclassid";

	public static final String TITLE = "title";

	public static final String CONTENT = "content";

	public static final String AUTHOR = "author";

	public static final String EDITOR = "editor";

	public static final String IMG = "img";

	public static final String CLICK_NUM = "clickNum";

	public static final String IS_TOP = "isTop";

	public static final String FILE_PATH = "filePath";

	public void attachClean(News instance) {
		log.debug("attaching clean News instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachDirty(News instance) {
		log.debug("attaching dirty News instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all News instances");
		try {
			String queryString = "from News";
			Query queryObject = getSession().createQuery(queryString);
			List li = queryObject.list();

			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);

			throw re;
		}
	}

	public List findByAuthor(Object author) {
		return findByProperty(AUTHOR, author);
	}

	public List findByClickNum(Object clickNum) {
		return findByProperty(CLICK_NUM, clickNum);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByEditor(Object editor) {
		return findByProperty(EDITOR, editor);
	}

	public List findByExample(News instance) {
		log.debug("finding News instance by example");
		try {
			List results = getSession().createCriteria("com.fick.hibernate.model.News").add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByFilePath(Object filePath) {
		return findByProperty(FILE_PATH, filePath);
	}

	public List findByHql(String hql) {

		try {
			String queryString = "from News " + hql;
			Query query = getSession().createQuery(queryString);
			List li = query.list();
			return li;
		} catch (RuntimeException e) {
			e.printStackTrace();
			return null;
		} finally {
			closeSession();
		}

	}

	public News findById(java.lang.Integer id) {
		log.debug("getting News instance with id: " + id);
		try {
			News instance = (News) getSession().get("com.fick.hibernate.model.News", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findByImg(Object img) {
		return findByProperty(IMG, img);
	}

	public List findByIsTop(Object isTop) {
		return findByProperty(IS_TOP, isTop);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding News instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from News as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findBySmallclassid(Object smallclassid) {
		return findByProperty(SMALLCLASSID, smallclassid);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findPageByHql(String hql, Integer currPage, Integer pageSize) {
		log.debug("find all new by page  currPage is error");
		try {
			String queryString = "from News " + hql;
			log.warn(queryString);
			Query query = getSession().createQuery(queryString);
			query.setFirstResult((currPage - 1) * pageSize);
			query.setMaxResults(pageSize);
			List li = query.list();
			return li;
		} catch (RuntimeException re) {
			log.error("find all news by page failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findPageFirstByHql(String hql, Integer pageSize, int first) {
		log.debug("find all new by page  currPage is error");
		try {
			String queryString = "from News " + hql;
			log.warn(queryString);
			Query query = getSession().createQuery(queryString);
			query.setFirstResult(first);
			query.setMaxResults(pageSize);
			List li = query.list();
			return li;
		} catch (RuntimeException re) {
			log.error("find all news by page failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public List findSearchPageByHql(String hql, Integer currPage, Integer pageSize) {
		log.debug("find all new by page  currPage is error");
		try {
			String queryString = "from News " + hql;
			int i3 = currPage;
			int i2 = pageSize;
			Query query = getSession().createQuery(queryString);
			query.setFirstResult((currPage - 1) * pageSize);
			query.setMaxResults(pageSize);
			List li = query.list();
			return li;
		} catch (RuntimeException re) {
			log.error("find all news by page failed", re);
			throw re;
		} finally {
			closeSession();
		}
	}

	public News merge(News detachedInstance) {
		log.debug("merging News instance");
		try {
			News result = (News) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
}